1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import sun.misc.FpUtils;
32 import sun.misc.DoubleConsts;
33
34 public class Rint {
35
36 static int testRintCase(double input, double expected) {
37 int failures = 0;
38 double result;
39 failures += Tests.test("Math.rint", input, Math.rint(input), expected);
40 failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);
41 failures += Tests.test("StrictMath.rint",
42 input, StrictMath.rint(input), expected);
43 failures += Tests.test("StrictMath.rint", -input,
44 StrictMath.rint(-input), -expected);
45 return failures;
46 }
47
48
49 public static void main(String args[]) {
50 int failures = 0;
51 double twoToThe52 = FpUtils.scalb(1.0, 52);
52
53 double [][] testCases = {
54 {0.0, 0.0},
55 {Double.MIN_VALUE, 0.0},
56 {FpUtils.nextDown(DoubleConsts.MIN_NORMAL), 0.0},
57 {DoubleConsts.MIN_NORMAL, 0.0},
58
59 {0.2, 0.0},
60
61 {FpUtils.nextDown(0.5), 0.0},
62 { 0.5, 0.0},
63 { FpUtils.nextUp(0.5), 1.0},
64
65 {0.7, 1.0},
66 {FpUtils.nextDown(1.0), 1.0},
67 { 1.0, 1.0},
68 { FpUtils.nextUp(1.0), 1.0},
69
70 {FpUtils.nextDown(1.5), 1.0},
71 { 1.5, 2.0},
72 { FpUtils.nextUp(1.5), 2.0},
73
74 {4.2, 4.0},
75 {4.5, 4.0},
76 {4.7, 5.0},
77
78 {7.5, 8.0},
79 {7.2, 7.0},
80 {7.7, 8.0},
81
82 {150000.75, 150001.0},
83 {300000.5, 300000.0},
84 {FpUtils.nextUp(300000.5), 300001.0},
85 {FpUtils.nextDown(300000.75), 300001.0},
86 {300000.75, 300001.0},
87 {FpUtils.nextUp(300000.75), 300001.0},
88 {300000.99, 300001.0},
89 {262144.75, 262145.0},
90 {499998.75, 499999.0},
91 {524287.75, 524288.0},
92 {524288.75, 524289.0},
93
94 {FpUtils.nextDown(twoToThe52), twoToThe52},
95 {twoToThe52, twoToThe52},
96 {FpUtils.nextUp(twoToThe52), FpUtils.nextUp(twoToThe52)},
97
98 {Double.MAX_VALUE, Double.MAX_VALUE},
99 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
100 {Double.NaN, Double.NaN}
101
102 };
103
104
105 for(int i = 0; i < testCases.length; i++) {
106 failures += testRintCase(testCases[i][0], testCases[i][1]);
107 }
108
109
110 for(double d = Double.MIN_VALUE;
111 d < Double.POSITIVE_INFINITY; d *= 2) {
112 failures += testRintCase(d, ((d<=0.5)?0.0:d));
113 }
114
115 if (failures > 0) {
116 System.err.println("Testing {Math, StrictMath}.rint incurred "
117 + failures + " failures.");
118 throw new RuntimeException();
119 }
120 }
121
122 }